﻿using BLL.Services;
using FlowerShop.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FlowerShopMVC.Controllers
{
    public class CustomerController : Controller
    {
        private readonly Customers _customerService;

        public CustomerController()
        {
            _customerService = new Customers();
        }

        public ActionResult Index()
        {
            var customers = _customerService.GetAllCustomers();
            return View(customers);
        }

        public ActionResult Details(int id)
        {
            var customer = _customerService.GetCustomerById(id);
            if (customer == null)
            {
                return HttpNotFound("未找到客户");
            }
            return View(customer);
        }

        // GET: Customer/Edit
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return HttpNotFound("客户 ID 不能为空");
            }

            var customer = _customerService.GetCustomerById(id.Value);
            if (customer == null)
            {
                return HttpNotFound("未找到客户");
            }
            return View(customer);
        }

        // POST: Customer/Edit
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Tpeople customer)
        {
            if (ModelState.IsValid)
            {
                _customerService.UpdateCustomer(customer);
                return RedirectToAction("Index");
            }
            return View(customer);
        }
    }
}